<?php

/**
 * asignaciones actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage asignaciones
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class asignacionesActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->valors = Doctrine_Core::getTable('valor')
                ->createQuery('a')
                ->execute();
    }

    public function executeSeleccion(sfWebRequest $request) {
        $this->idpro = $request->getParameter('id_pro');
    }

    public function executeGraba2(sfWebRequest $request) {

        if (isset($_POST['descripcion']) && isset($_POST['actividad']) && isset($_POST['mi']) && isset($_POST['ai']) && isset($_POST['mf']) && isset($_POST['af']) && isset($_POST['valor'])) {
            $idac = mysql_escape_String($_POST['actividad']);

            $iddg = mysql_escape_String($_POST['descripcion']);
            $mi = mysql_escape_String($_POST['mi']);
            $ai = mysql_escape_String($_POST['ai']);
            $mf = mysql_escape_String($_POST['mf']);
            $af = mysql_escape_String($_POST['af']);
            $valor = mysql_escape_String($_POST['valor']);
            $q = Doctrine_Query::create()
                    ->from('descripciongeneral d')
                    ->where('d.id_dg = ?', $iddg);
            $des = $q->fetchOne();
            $idpro = $des->getIdPro();
            $q2 = Doctrine_Query::create()
                    ->from('fondo f')
                    ->where('f.id_pro = ?', $idpro);
            $fo = $q2->fetchOne();
            $idfo = $fo->getIdFo();
            $valores = Doctrine::getTable('valor')->createQuery('v')
                    ->where('v.id_fo = ?', $idfo)
                    ->andwhere('v.id_ac = ?', $idac)
                    ->andwhere('v.id_dg = ?', $iddg)
                    ->execute();

            if ($ai == $af) {
                foreach ($valores as $va):
                    if ($va->getAnio() == $ai) {
                        if ($va->getMes() >= $mi && $va->getMes() <= $mf) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    }
                    if ($va->getAnio() == $af) {
                        if ($va->getMes() >= $mi && $va->getMes() <= $mf) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    }

                endforeach;
            } else {
                $aux = 0;
                $aux = $af - $ai;
                if ($aux == 1) {
                    foreach ($valores as $va):
                        if ($va->getMes() >= $mi && $va->getAnio() == $ai) {
                            $va->setVal($valor);
                            $va->save();
                        }
                        if ($va->getMes() <= $mf && $va->getAnio() == $af) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    endforeach;
                } else {
                    foreach ($valores as $va):
                        if ($va->getMes() >= $mi && $va->getAnio() == $ai) {
                            $va->setVal($valor);
                            $va->save();
                        }

                        if ($va->getAnio() > $ai && $va->getAnio() < $af) {
                            $va->setVal($valor);
                            $va->save();
                        }

                        if ($va->getMes() <= $mf && $va->getAnio() == $af) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    endforeach;
                }
            }
        }
    }

    public function executeGraba(sfWebRequest $request) {

        if (isset($_POST['fondo']) && isset($_POST['descripcion']) && isset($_POST['actividad']) && isset($_POST['mi']) && isset($_POST['ai']) && isset($_POST['mf']) && isset($_POST['af']) && isset($_POST['valor'])) {


            $idac = mysql_escape_String($_POST['actividad']);
            $idfo = mysql_escape_String($_POST['fondo']);
            $iddg = mysql_escape_String($_POST['descripcion']);
            $mi = mysql_escape_String($_POST['mi']);
            $ai = mysql_escape_String($_POST['ai']);
            $mf = mysql_escape_String($_POST['mf']);
            $af = mysql_escape_String($_POST['af']);
            $valor = mysql_escape_String($_POST['valor']);

//            $q = Doctrine_Query::create()
//                    ->from('valor v')
//                    ->where('v.id_fo = ?', $idfo)
//                    ->andwhere('v.id_ac = ?', $idac)
//                    ->andwhere('v.id_dg = ?', $iddg)                    
//                    ->andwhere('v.mes = ?', $mi)
//                    ->andwhere('v.anio = ?', $ai);
//            $val = $q->fetchOne();
//            $val->setVal($valor);
//            $val->save();

            $valores = Doctrine::getTable('valor')->createQuery('v')
                    ->where('v.id_fo = ?', $idfo)
                    ->andwhere('v.id_ac = ?', $idac)
                    ->andwhere('v.id_dg = ?', $iddg)
                    ->execute();

            if ($ai == $af) {
                foreach ($valores as $va):
                    if ($va->getAnio() == $ai) {
                        if ($va->getMes() >= $mi && $va->getMes() <= $mf) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    }
                    if ($va->getAnio() == $af) {
                        if ($va->getMes() >= $mi && $va->getMes() <= $mf) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    }

                endforeach;
            } else {
                $aux = 0;
                $aux = $af - $ai;
                if ($aux == 1) {
                    foreach ($valores as $va):
                        if ($va->getMes() >= $mi && $va->getAnio() == $ai) {
                            $va->setVal($valor);
                            $va->save();
                        }
                        if ($va->getMes() <= $mf && $va->getAnio() == $af) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    endforeach;
                } else {
                    foreach ($valores as $va):
                        if ($va->getMes() >= $mi && $va->getAnio() == $ai) {
                            $va->setVal($valor);
                            $va->save();
                        }

                        if ($va->getAnio() > $ai && $va->getAnio() < $af) {
                            $va->setVal($valor);
                            $va->save();
                        }

                        if ($va->getMes() <= $mf && $va->getAnio() == $af) {
                            $va->setVal($valor);
                            $va->save();
                        }
                    endforeach;
                }
            }
        }




        $this->redirect('proyectos/index');
    }

    public function executeOpcion1(sfWebRequest $request) {
        $this->idpro = $request->getParameter('id_pro');
        $q = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro=?', $this->idpro);
        $this->proyecto = $q->fetchOne();

        $this->objetivos = Doctrine::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->idpro)
                ->execute();
        $this->fondos = Doctrine::getTable('fondo')->createQuery('f')
                ->where('f.id_pro = ?', $this->idpro)
                ->execute();

        $this->techos = Doctrine::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->idpro)
                ->execute();
    }

    public function executeOpcion2(sfWebRequest $request) {
        $this->idpro = $request->getParameter('id_pro');
        $q = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro=?', $this->idpro);
        $this->proyecto = $q->fetchOne();

        $this->objetivos = Doctrine::getTable('objetivo')->createQuery('o')
                ->where('o.id_pro = ?', $this->idpro)
                ->execute();
        $this->fondos = Doctrine::getTable('fondo')->createQuery('f')
                ->where('f.id_pro = ?', $this->idpro)
                ->execute();

        $this->techos = Doctrine::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $this->idpro)
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va')));
        $this->forward404Unless($this->valor);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new valorForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new valorForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va'))), sprintf('Object valor does not exist (%s).', $request->getParameter('id_va')));
        $this->form = new valorForm($valor);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va'))), sprintf('Object valor does not exist (%s).', $request->getParameter('id_va')));
        $this->form = new valorForm($valor);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($valor = Doctrine_Core::getTable('valor')->find(array($request->getParameter('id_va'))), sprintf('Object valor does not exist (%s).', $request->getParameter('id_va')));
        $valor->delete();

        $this->redirect('asignaciones/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $valor = $form->save();

            $this->redirect('asignaciones/edit?id_va=' . $valor->getIdVa());
        }
    }

}
